package com.lqq.eduorder.service.impl;

import com.lqq.commonutils.vo.CourseOrderVo;
import com.lqq.commonutils.vo.UcenterOrderVo;
import com.lqq.eduorder.client.OrderCourseClient;
import com.lqq.eduorder.client.OrderUcenterClient;
import com.lqq.eduorder.entity.Order;
import com.lqq.eduorder.mapper.OrderMapper;
import com.lqq.eduorder.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lqq.eduorder.utils.OrderNoUtil;
import com.lqq.servicebase.exceptionHandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author 677
 * @since 2021-07-12
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderUcenterClient ucenterClient;

    @Autowired
    private OrderCourseClient courseClient;
    //1.生成订单
    @Override
    public String createOrder(String courseId, String memberId) {
        //远程调用ucenter返回用户信息
        UcenterOrderVo ucenterInfo = ucenterClient.getUcenterOrderInfo(memberId);
        //远程调用根据课程id返回课程信息
        CourseOrderVo courseInfo = courseClient.getCourseInfoOrder(courseId);

        //插入数据库
        Order order = new Order();
        order.setOrderNo(OrderNoUtil.getOrderNo());//生成随机订单号
        order.setCourseId(courseId);
        order.setCourseTitle(courseInfo.getTitle());
        order.setCourseCover(courseInfo.getCover());
        order.setTeacherName(courseInfo.getTeacherName());
        order.setTotalFee(courseInfo.getPrice());
        order.setMemberId(memberId);
        order.setMobile(ucenterInfo.getMobile());
        order.setNickname(ucenterInfo.getNickname());
        order.setStatus(0);
        order.setPayType(1);
        int insert = baseMapper.insert(order);
        if (insert == 0) {
            throw new GuliException(200001, "生成订单失败！");
        }

        //插入成功，返回订单id
        return order.getOrderNo();
    }
}
